﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using School.Entities;

namespace School.Pages.Uniforms
{
    public partial class SRV_Clothe_Stock_Record : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1405) == true)
                {

                    if (!this.Page.IsPostBack)
                    {
                        btnSaveStock.Visible = false;
                    }
                    lblResult.Text = "";
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");

            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {   
            if (CampusPackage1.CampusId != "0" && CampusPackage1.CertificateId != "0" && CampusPackage1.GradeId != "0" && CampusPackage1.StageId != "0")
            {
                Hashtable table = new Hashtable();
                table.Add("Cer_Id", CampusPackage1.CertificateId);
                table.Add("Campus_Id", CampusPackage1.CampusId);
                table.Add("Stage_Id", CampusPackage1.StageId);
                table.Add("Grad_Id", CampusPackage1.GradeId);
                IEnumerable<object> lst = School.Service.Clothes.SRV_Clothe_Sizes_Size.SizeSearch(int.Parse(CampusPackage1.CampusId), int.Parse(CampusPackage1.CertificateId),
                    int.Parse(CampusPackage1.StageId), int.Parse(CampusPackage1.GradeId));
              
                if (lst.Count() > 0)
                {
                     btnSaveStock.Visible = true;
                    btnSaveStock.Text = "حساب الفرق";
                }
                gvdata.DataSource = lst;
                gvdata.DataBind();
            }
        }

        protected void btnSaveStock_Click(object sender, EventArgs e)
        {
            if (btnSaveStock.Text == "حساب الفرق")
            {
                foreach (GridViewRow row in gvdata.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox txtQty = (TextBox)row.FindControl("txtQty");
                        TextBox txtDifferance = (TextBox)row.FindControl("txtDifferance");
                        if (!string.IsNullOrEmpty(row.Cells[4].Text.ToString()) && (!string.IsNullOrEmpty(txtQty.Text)))
                            txtDifferance.Text =(int.Parse(txtQty.Text) - int.Parse(row.Cells[4].Text) ).ToString();
                        else
                        {
                            lblResult.Text = "يجب ادخال كافه القيم";
                            return;
                        }
                    }
                }
                btnSaveStock.Text = "تسجيل";
            }
            else
            {
                List<SRV_CLOTHES_STOCKTACKING> lstStock = new List<SRV_CLOTHES_STOCKTACKING>();
                foreach (GridViewRow row in gvdata.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox txtQty = (TextBox)row.FindControl("txtQty");
                        TextBox txtDifferance = (TextBox)row.FindControl("txtDifferance");
                        long SystemQty = 0;
                        if(!string.IsNullOrEmpty(row.Cells[4].Text))
                            SystemQty = int.Parse(row.Cells[4].Text);
                        long physicalQty = 0;
                          if(!string.IsNullOrEmpty(txtQty.Text))
                              physicalQty = int.Parse(txtQty.Text);
                         lstStock.Add(new SRV_CLOTHES_STOCKTACKING()
                        {
                            Date_Time = DateTime.Now,
                            Size_Id = int.Parse(gvdata.DataKeys[row.RowIndex].Value.ToString()),
                            System_Qty = SystemQty,
                            Pysical_Qty = physicalQty,
                         });
                    }
                }
                if (School.Service.Uniform.SRV_CLOTHES_STOCKTACKINGClass.SRV_CLOTHES_STOCKTACKINGInsert(lstStock))
                {
                    lblResult.Text = "تمت عمليه التسجيل بنجاح";
                    ClearData();
                }

                else
                    lblResult.Text = "هناك خطا فى ادخال البيانات";
            }
        }

        private void ClearData()
        {
            foreach (GridViewRow row in gvdata.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    ((TextBox)row.FindControl("txtQty")).Text = "";
                    ((TextBox)row.FindControl("txtDifferance")).Text = "" ;
                }
            }
        }
    }
}